//加密函数
import { encryption } from "@/utils/password.js";
import { date } from "@/utils/date.js";
import { Toast } from "vant";
import { Dialog } from "vant";
import router from "../router";
export var my = {
  getSession: function(name) {
    if (!name) return;
    return window.sessionStorage.getItem(name);
  },
  setSession: function(name, content) {
    if (!name) return;
    if (typeof content !== "string") {
      content = JSON.stringify(content);
    }
    window.sessionStorage.setItem(name, content);
  },
  removeSession: function(name) {
    if (!name) return;
    window.sessionStorage.removeItem(name);
  },
  getLocal: function(name) {
    if (!name) return;
    let val =  window.localStorage.getItem(name);
    if ( val == 'undifined' ) {
      val = ''
    }
    try{
      return JSON.parse(val);
    }catch(e) {
      return val;
    }
  },
  setLocal: function(name, content) {
    if (!name) return;
    if (typeof content !== "string") {
      content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
  },
  setCookie:function(c_name,value,expiredays) {
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  },
  getCookie:function(c_name) {
    if (document.cookie.length>0)
    {
      var  c_start=document.cookie.indexOf(c_name + "=")
      if (c_start!=-1)
      {
        c_start=c_start + c_name.length+1
        var c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1) c_end=document.cookie.length
        return unescape(document.cookie.substring(c_start,c_end))
      }
    }
    return ""
  },
  removeLocal: function(name) {
    if (!name) return;
    window.localStorage.removeItem(name);
  },
  clearLocal: function() {
    window.localStorage.clear();
  },
  in_array: function(search, array) {
    for (var i in array) {
      if (array[i] == search) {
        return true;
      }
    }
    return false;
  },
  // 去除
  rtrim: function(str, search = " ") {
    var i;
    for (i = str.length - 1; i >= 0; i--) {
      if (str.charAt(i) != search && str.charAt(i) != search) {
        break;
      }
    }
    str = str.substring(0, i + 1);
    return str;
  },
  //加密
  encode: function(password) {
    return encryption(password);
  },
  date: function(format, timestamp) {
    return date(format, timestamp);
  },
  //提示
  toast: function(obj, sec = 3, fn = null) {
    const toast = Toast(obj);
    const timer = setInterval(() => {
      sec--;
      if (!sec) {
        clearInterval(timer);
        Toast.clear();
        if (fn) {
          fn();
        }
      }
    }, 1000);
  },
  //请登录
  checkLogin: function(msg = "该页面需要登录授权，请先登录", title = "提示") {
    Dialog.confirm({
      title: title,
      message: msg
    })
      .then(() => {
        router.push({
          path: "/login"
        });
      })
      .catch(() => {});
  },
  notice: function(
    url = "",
    title = "操作成功",
    urltitle = "确定",
    icon = "success"
  ) {
    router.push({
      path: "/notice",
      query: { url: url, title: title, urltitle: urltitle, icon: icon }
    });
  },
  //判断是否是微信浏览器
  isWeiXin:function(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
      return true;
    }else{
      return false;
    }
  },
  dataURItoBlob:function(dataURI) {
    var byteString = atob(dataURI.split(',')[1]);
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], {type: mimeString});
  }

};
